/*
 */
package org.vectrics.recruiter.company;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.vectrics.DataException;
import org.vectrics.SystemException;
import org.vectrics.company.Company;
import org.vectrics.company.CompanyPersonGroupType;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.Filter;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.PersonSearchCriteria;


/**
 * @author mmoore
 */
public class RecruitCompanyPersistMgr extends RecruitCompanyService {
    private transient final Log log = LogFactory.getLog(this.getClass());
	
	public RecruitCompanyPersistMgr() {
	}

	/**
	 * Find the recruiting information specific to a company.
	 * compay The compan.
	 */
    public RecruitCompanyInfo findRecruitCompanyInfo(Company company) {
		RecruitCompanyInfo info = null;
		
		Filter filter = new Filter();
		filter.addFrom("RecruitCompanyInfo", "recruitCompanyInfo");
		filter.addSelectField("recruitCompanyInfo");
		filter.addParameterCondition("recruitCompanyInfo.company.id", "=", company.getId());
		Session session = ThreadProperties.getSession();
		
		Query query = filter.createQuery(session);

		Iterator iter = query.iterate();
		if (iter.hasNext()) {
			info = (RecruitCompanyInfo)iter.next();
		}
		return(info);
    }

    /**
     * Save or update the recruiting information for a company.
     */
    public void updateRecruitCompanyInfo(RecruitCompanyInfo recruitCompanyInfo) {
    	if (log.isDebugEnabled())
    		log.debug("Save or update recruit company info in database: " + recruitCompanyInfo);
    	
    	if (recruitCompanyInfo.getCompany() == null)
    		throw new SystemException("RecruitCompanyInfo assigned a null company");
    	if (recruitCompanyInfo.getCompany().getId() == null)
    		throw new SystemException("RecruitCompanyInfo assigned a company that has not been saved");
		
    	recruitCompanyInfo.setDateLastChanged(new Date());
		try {
			Session session = ThreadProperties.getSession();
			session.saveOrUpdate(recruitCompanyInfo);
		}
		catch (Exception he) {
			throw new DataException(he);
		}
	}

/*
    public void addJobCandidate(Company company, Person person) {
		CompanyPerson companyPerson = CoreServiceLocator.getCompanyService().findCompanyPerson(company, person);
		if (companyPerson == null) {
			companyPerson = new CompanyPerson();
			companyPerson.setCompany(company);
			companyPerson.setPerson(person);
			companyPerson.setDateStart(DateSync.getCurrentTime());
		}
		companyPerson.setDateEnd(null);
		
	}
*/
    
	public List findHiringManagers(Company company) {
		List resultSet = new ArrayList();
		PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
		Company currentCompany = company;
		while (currentCompany != null) {
			criteria.addCompany(company);
			CompanyPersonGroupType groupType = CoreServiceLocator.getCompanyService().findCompanyGroupType(RecruitCompanyService.GROUP_HIRING_MANAGER);
			criteria.addGroupType(groupType);
			criteria.addCompany(currentCompany);
			currentCompany = currentCompany.getParent();
		}
		resultSet.addAll(CoreServiceLocator.getPersonService().findPersons(criteria, 0, 100));
		return(resultSet);
	}

	/**
	 * Find the recruiters for the passed company.
	 */
	public List findRecruiters(Company company) {
		PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
		Company currentCompany = company;
		Domain currentDomain = currentCompany.getDomain();
		//criteria.addDomain(currentDomain);
		CompanyPersonGroupType groupType = CoreServiceLocator.getCompanyService().findCompanyGroupType(RecruitCompanyService.GROUP_RECRUITER);
		criteria.addGroupType(groupType);
		while ((currentCompany != null) && (currentCompany.getDomain().getId().equals(currentDomain.getId()))) {
			criteria.addCompany(currentCompany);
			currentCompany = currentCompany.getParent();
		}
		List results = CoreServiceLocator.getPersonService().findPersons(criteria, 0, 100);
		return(results);
		
	}
	
}
